const jwt = require("jsonwebtoken")
var md5 = require('md5');
const multer = require("multer");
const path = require("path");

const storage = multer.diskStorage({
    destination:function(req,file,cb){
        cb(null,__dirname + '/../public/static/uploads')
    },
    // 上传到服务器的文件
    filename:function(req,file,cb){
        
        const basename = path.basename(file.originalname,path.extname(file.originalname))
        const extname = path.extname(file.originalname)
        const newName = basename + new Date().getTime() + extname
        cb(null,newName)
    }
})
// const upload = multer({
//     storage,
//     limits:{
//         fieldSize:2000000,
//         files
//     }
// })
module.exports.uploading = multer({
    storage,
    limits:{
        fieldSize:2000000,
        files:1
    }
})


//格式化出参
module.exports.formatResponse = function(code,msg,data){
    return {
        code,
        msg,
        data
    }
}

module.exports.analysisToken = function(token){
    return jwt.verify(token.split(" ")[1],md5(process.env.JWT_SECRET))
}